package org.skf.plan

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.skf.plan.ui.theme.All4PlanTheme
import timber.log.Timber

class MainActivity : ComponentActivity() {
    private val viewModel by viewModels<V>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            All4PlanTheme {
                // A surface container using the 'background' color from the theme
                Surface(color = MaterialTheme.colors.background) {
                    Greeting("Android")
                }
            }
        }
        Timber.e("${viewModel.toString()}")
    }
}

@Composable
fun Greeting(name: String) {
    Text(text = "Hello $name!")
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    All4PlanTheme {
        Greeting("Android")
    }
}

class V : ViewModel() {
    init {
        viewModelScope.launch(Dispatchers.IO) {
            var x = 0
            while (true) {
                Timber.e("x = ${x++}")
                delay(100)
            }
        }
        viewModelScope.launch ( Dispatchers.Default ){
            var y =0
            while (true){
                Timber.e("y = ${y++}")
                delay(100)
            }
        }
    }
}